﻿using System;

using System.Data.SqlClient;

namespace IFD2
{
    public partial class BookOpenTrip : System.Web.UI.Page
    {
        private bool booked = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SB6") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }

                hidOrderID.Value = Request.QueryString["bk_id"];
                hidPaxSn.Value = Request.QueryString["pax_sn"];
                hidTripSn.Value = Request.QueryString["trip_sn"];
                try
                {
                    DateTime.Parse(Request.QueryString["flt_date"]);
                }
                catch(Exception ex)
                {
                    lblError.Text = "Invalid flight date!";
                    btnCurrDate.Enabled = false;
                    btnLastDate.Enabled = false;
                    btnNextDate.Enabled = false;
                    return;
                }
                hidFltDate.Value = Request.QueryString["flt_date"];//DateTime.Now.AddDays(1).ToShortDateString();
                btnCurrDate.Text = hidFltDate.Value;
                grdFlights.DataBind();
            }
            lblError.Text = "";
        }

        
        protected void btnBook_Click(object sender, EventArgs e)
        {
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            String sSeg = hidTripSn.Value + ",";
            String sFlt = strArray[1] + ",";
            String sDate = strArray[2] + ",";
            String sOri = strArray[3] + ",";
            String sDes = strArray[4] + ",";
            String sClass = strArray[6] + ",";

            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_change_flt");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderID.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@force_transfer", false);
                para = cmd.Parameters.AddWithValue("@count", 1);
                para = cmd.Parameters.AddWithValue("@seg_buf", sSeg.ToString());
                para = cmd.Parameters.AddWithValue("@flt_buf", sFlt.ToString());
                para = cmd.Parameters.AddWithValue("@date_buf", sDate.ToString());
                para = cmd.Parameters.AddWithValue("@ori_buf", sOri.ToString());
                para = cmd.Parameters.AddWithValue("@des_buf", sDes.ToString());
                para = cmd.Parameters.AddWithValue("@class_buf", sClass.ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    booked = true;
                    lblError.Text = "Done.";//成功返回空数据，造成冲掉成功提示！
                }
                else if (ret_code == 1)
                    lblError.Text = "Too many trips!";
                else if (ret_code == 2)
                    lblError.Text = "Bad format!";
                else if (ret_code == 3)
                    lblError.Text = "Target flight not found or not available!";
                else if (ret_code == 4)
                    lblError.Text = "Target flight segment not found!";
                else if (ret_code == 5)
                    lblError.Text = "Trip not found!";
                else if (ret_code == 6)
                    lblError.Text = "Cabin not found!";
                else if (ret_code == 7)
                    lblError.Text = "Class not found or not open!";
                else if (ret_code == 8)
                    lblError.Text = "No available seat!";
                else if (ret_code == 9)
                    lblError.Text = "Original city mismathed!";
                else if (ret_code == 20)
                    lblError.Text = "Book target cabin failed!";
                else if (ret_code == 21)
                    lblError.Text = "Book target class failed!";
                else if (ret_code == 22)
                    lblError.Text = "Cancel origin cabin failed!";
                else if (ret_code == 23)
                    lblError.Text = "Cancel class class failed!";
                else if (ret_code == 30)
                    lblError.Text = "Change ticket flight failed!";
                else if (ret_code == 102)
                    lblError.Text = "Transfer done. But re-calculating nested failed, please re-calculate related flights manually!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void grdFlights_DataBound(object sender, EventArgs e)
        {
            if (!booked)
            {
                if (grdFlights.Rows.Count > 0)
                    btnCurrDate.Text = Convert.ToDateTime(grdFlights.Rows[0].Cells[1].Text).ToShortDateString();//true query date!
                else
                    lblError.Text = "No available flight, you can try another date.";
            }
        }
        protected void btnNextDate_Click(object sender, EventArgs e)
        {
            hidFltDate.Value = Convert.ToDateTime(btnCurrDate.Text).AddDays(1).ToShortDateString();
            btnCurrDate.Text = hidFltDate.Value;
            grdFlights.DataBind();

        }
        protected void btnLastDate_Click(object sender, EventArgs e)
        {
            hidFltDate.Value = Convert.ToDateTime(btnCurrDate.Text).AddDays(-1).ToShortDateString();
            btnCurrDate.Text = hidFltDate.Value;
            grdFlights.DataBind();

        }
    }
}